package utilities.utilities_components;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Auxhead;
import org.zkoss.zul.Auxheader;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listfoot;
import org.zkoss.zul.Listfooter;
import org.zkoss.zul.Listhead;
import org.zkoss.zul.Listheader;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;
import org.zkoss.zul.Checkbox;

import java.util.Arrays;
import java.util.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import utilities.Updatable;

public class PlainList extends Window implements Updatable{
	private Listbox list;
	private Auxhead auxHead;
	private Auxheader auxHeader;
	private Listhead head;
	private Listheader header;
	private Listfoot foot;
	private Listfooter footer;
	private Listitem listItem;
	private Listcell cell;
	
	private int[] footerMapping;
	private int[] checkCols;
	private ArrayList checkList;
	private SelectListener listener;
	private List cols;
	
	public static interface SelectListener{
		public void onFocus(Component obj);
	}
	
	private static final int PAGE_SIZE = 15;
	
	public PlainList(){
		super();
		
		list = new Listbox();
		list.setMold( "paging" );
		this.setPagingSize( PAGE_SIZE );
		this.appendChild( list );
		
		head = new Listhead();
		list.appendChild( head );
		
		/*  Empty auxiliary head and is not appended into its parent component  */
		auxHead = new Auxhead();
		
		/*  Empty list foot, and is not appended into its parent component  */
		foot = new Listfoot();
		
		list.addEventListener(Events.ON_SELECT,new EventListener(){
			public void onEvent(Event e){
				if(listener != null)
					listener.onFocus(PlainList.this);
			}
		});
		
		cols=new LinkedList();
	}
	
	public void setBlur(){
		this.list.setSelectedIndex(-1);
		this.list.setFocus(false);
	}
	
	public void setListener(SelectListener listener){
		this.listener=listener;
	}
	
	public void setCheckShow(int[] cols){
		Arrays.sort(cols);
		this.checkCols=cols;
		
		if(checkList == null)
			checkList=new ArrayList();
		checkList.clear();
		for(int i=0;i < cols.length;i++)
			checkList.add(new ArrayList());
	}
	
	public void setFooter( String[] contents, int[] spans, int[] indices, int[] mapping ){
		list.appendChild( foot );
		
		footerMapping = mapping;
		
		int index = 0;
		for( int count = 0; count < spans.length; count++ ){
			footer = new Listfooter();
			footer.setSpan( spans[ count ] );
			foot.appendChild( footer );
			
			if( count == indices[ index ] ){
				footer.setLabel( contents[ index ] );
				index++;
			}
		}
	}
	
	public int getSelectedIndex(){
		return this.list.getSelectedIndex();
	}
	
	public void setHead( String[] metaInfo ){
			for( int count = 0; count < metaInfo.length; count++ ){
				header = new Listheader( metaInfo[ count ] );
				header.setAlign( "center" );
				head.appendChild( header );
			}
	}
	
	public void setSortable( int[] sortable ){
		List sortIndex = head.getChildren();
		for( int count = 0; count < sortable.length; count++ ){
			( ( Listheader )sortIndex.get( sortable[ count ] ) ).setSort( "auto" );
		}
	}
	
	public void setPagingSize( int size ){
		list.setPageSize( size );
		list.setRows( size );
	}
	
	public Set getSelectedItems(){
		return list.getSelectedItems();
	}
	
	public int getIndexOfItem( Listitem item ){
		return list.getIndexOfItem( item );
	}
	
	public void update( List data ){
		cols.clear();
		if( data == null ){
			try{
				new Messagebox().show( "更新失败!" );				
			}catch( InterruptedException exception ){
				/*  Ignore  */
			}
			
			return;
		}
		
		clear();
		
		Iterator rowItr = data.iterator();
		
		while( rowItr.hasNext() ){
			LinkedList newCol=new LinkedList();
			cols.add(newCol);
			for(int i=0;i < checkList.size();i++)
				((ArrayList)checkList.get(i)).clear();
			
			listItem = new Listitem();
			list.appendChild( listItem );
			
			List rowData = ( List )rowItr.next();
			Iterator cellItr = rowData.iterator();
			
			int i=0;
			while( cellItr.hasNext() ){
				 Object obj=cellItr.next();
				 int key=Arrays.binarySearch(checkCols,i);
				 if(key < 0 || !(obj instanceof Boolean))
				 	cell = new Listcell( obj.toString());
				 else{
					 cell=new Listcell();
					 Checkbox box=new Checkbox();
					 box.setChecked((Boolean)obj);
					 ((ArrayList)checkList.get(i)).add(box);
				 }
				 newCol.add(cell);
				 listItem.appendChild( cell );
				 i++;
			}
		}
	}
	
	public List getColDate(int col){
		if(col >= cols.size())return null;
		else return (List)cols.get(col);
	}
	
	public Boolean getCheckInfo(int col,int index){
		index=Arrays.binarySearch(checkCols,index);
		if(index >= 0 && ((ArrayList)checkList.get(index)).size() > col)
			return ((Checkbox)((ArrayList)checkList.get(index)).get(col)).isChecked();
		else return null;
	}
	
	public void setDisable( boolean disabled ){
		/*  Empty method body  */
	}
	
	public void clear(){
		List children = list.getChildren();
		int end = 2;
		
		if( foot != children.get( children.size() - 1 ) ){
			end = 1;
		}
		
		int count = children.size() - end;
		
		for( ; count > 0; count-- ){
			list.removeItemAt( 1 );
		}
	}
	
	public void setMultiple( boolean multi ){
		list.setMultiple( multi );
	}
}
